package cn.tedu.recruitmentwebsite.common.service.impl;

import cn.tedu.recruitmentwebsite.base.exception.ServiceException;
import cn.tedu.recruitmentwebsite.base.pojo.dto.UpdatePasswordParam;
import cn.tedu.recruitmentwebsite.base.pojo.dto.UserLoginParam;
import cn.tedu.recruitmentwebsite.base.pojo.dto.UserSaveParam;
import cn.tedu.recruitmentwebsite.base.pojo.entity.User;
import cn.tedu.recruitmentwebsite.base.pojo.vo.UserVO;
import cn.tedu.recruitmentwebsite.base.response.StatusCode;
import cn.tedu.recruitmentwebsite.common.mapper.CommonMapper;
import cn.tedu.recruitmentwebsite.common.service.CommonService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class CommonServiceImpl implements CommonService {
    @Autowired
    private CommonMapper commonmapper;
    @Override
    public User login(UserLoginParam userLoginParam) {
        log.info("用户登录，参数：{}", userLoginParam);
        if (userLoginParam.getPhone() == null||userLoginParam.getPhone().isEmpty()) {
            String username = userLoginParam.getUsername();
            User user = commonmapper.selectByUsername(username);
            if (user == null){
                throw new ServiceException(StatusCode.USERNAME_ERROR);
            }else {
                if (user.getPassword().equals(userLoginParam.getPassword())){
                    return user;
                }else {
                    throw new ServiceException(StatusCode.PASSWORD_ERROR);
                }
            }
        } else {
            //校验手机号
            String phone = userLoginParam.getPhone();
            User user = commonmapper.selectByPhone(phone);
            if (user == null) {
                throw new ServiceException(StatusCode.PHONE_ERROR);
            }else {
                return user;
            }
        }
    }

    @Override
    public void reg(UserSaveParam userSaveParam) {
        log.info("用户注册，参数：{}", userSaveParam);
        User user =new User();
        BeanUtils.copyProperties(userSaveParam,user);
        String nickname= "用户"+ UUID.randomUUID().toString().substring(0,8);
        user.setNickname(nickname);
        user.setGender(0);
        commonmapper.insert(user);
    }

    @Override
    public void updateLastLoginTime(Long id) {
        log.info("修改用户最后一次登录时间，参数：{}", id);
        commonmapper.updateLastLoginTime(id);
    }

    @Override
    public void changePassword(UpdatePasswordParam updatePasswordParam) {
        User user = commonmapper.selectByUsername(updatePasswordParam.getUsername());
        log.info("查询到的用户：{}", user);
        if (user.getPassword().equals(updatePasswordParam.getOldPassword())){
           commonmapper.updatePassword(user.getId(),updatePasswordParam.getNewPassword());
           log.info("修改密码成功");

        }else {
            log.info("旧密码错误");
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
    }
}
